// pages/composition/index.js
var model=require('./model/article.js')
var banner=require('../index/model/BannerModel.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    _currentLevel:{},
    _changeLevelState:false,
    compositionTypes:[],
    currentTypeId:-1,
    boutiqueList:[],
    normalList:[],
    banners:[]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that=this
    var globalUser = getApp().globalData.userInfo;
    if (globalUser!=null){
      var gradeId = globalUser.gradeLevel
      if (gradeId == null || typeof (gradeId) == 'undefined') {
        gradeId = 2;
      }
      var gradeText = globalUser.gradeLevelText
      if (gradeText == null || typeof (gradeText) == 'undefined') {
        gradeText = "一年级(上)" ;
      }
      this.setData({
        _currentLevel: { id: gradeId, text: gradeText }
      })
    }else{
      this.setData({
        _currentLevel: { id: 2, text: "一年级(上)" }
      })
    }
    model.loadCompositionTypes(function(list){
      var tmpList=[];
      tmpList.push({ id: -1, text: "全部",select:true})
      list.forEach(function(item,index){
        if (item.id != -1 || item.text!=='未知类型'){
          item['select']=false
          tmpList.push(item)
        }
      })
      that.setData({
        compositionTypes: tmpList
      })
    })
    this.loadData(this.data._currentLevel.id,null)
    banner.getBanners("0", function (data) {
      that.setData({
        banners: data
      })
    })
  },
  selectType:function(event){
    var id = event.currentTarget.dataset.value;
    var types = this.data.compositionTypes;
    types.forEach(function(item,index){
      if (item.id==id){
        item.select=true
      }else{
        item.select = false
      }
    })
    if (id != this.data.currentTypeId){
      this.loadData(this.data._currentLevel.id, id)
    }
    this.setData({
      currentTypeId:id,
      compositionTypes: types
    })
   
  },
  choseLocation:function(){
    wx.navigateTo({
      url: '../grade/list?gradeId=' + this.data._currentLevel.id + '&gradeText=' + this.data._currentLevel.text,
    })
  },
  loadData:function(grade,type){
    var that=this
    model.loadIndexList(grade, type, null, function (recommList, allList) {
      // console.log("recommList:%o:", recommList)
      // console.log("allList:%o:", allList)
      that.setData({
        boutiqueList: recommList,
        normalList: allList
      })
    })
  },
  seeDetail:function(event){
    var position = event.currentTarget.dataset.position
    var flag = event.currentTarget.dataset.flag
    var obj=null;
    if (flag ==='boutique'){
      obj=this.data.boutiqueList[position]
    } else if (flag ==='normal'){
      obj = this.data.normalList[position]
    }
    obj['des']='';
    if(obj!=null){
      wx.navigateTo({
        url: '../article-detail/detail?baseInfo=' + JSON.stringify(obj),
      })
    }
  },
  moreJingPing:function(){
    wx.navigateTo({
      url: '../article-more/more?gradeObject=' + JSON.stringify(this.data._currentLevel) + "&boutique=1&type=" + this.data.currentTypeId,
    })
  },
  moreNormal:function(){
    wx.navigateTo({
      url: '../article-more/more?gradeObject=' + JSON.stringify(this.data._currentLevel) + "&boutique=0&type=" + this.data.currentTypeId,
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if (this.data._changeLevelState){
      this.loadData(this.data._currentLevel.id, this.data.currentTypeId)
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})